<?php

class news
{

    function news() 
    {
    
    }
    /**
     * Validate news data
     */
    function validateNews()
    {
    	$msg = "";	
 	
		if($_POST['news_title'] == "")
		{
			if($msg != "") $msg .= "<br />";
			$msg .= _("Missing news title.");
		}
		
		if($_POST['news_content'] == "")
		{
			if($msg != "") $msg .= "<br />";
			$msg .= _("Missing news content.");
		}
		
		return $msg;
    }
    
      /**
     * Save news.
     * news is array with format. See smarty template file of news for
     * form's element name.
     */
    function saveNews($db, $news, $langid)
    {
    	$res = $db->query(sprintf(
					"INSERT INTO %s(news_image, news_date) " .
					"VALUES('%s',now())",
    				TBL_NEWS, 
    				$news['news_image']));
    	
    	$id = $db->insert_id;
		$country = $db->get_results("SELECT * FROM ".TBL_LANG);	
		foreach($country as $item){	
			$db->query(sprintf(
					"INSERT INTO %s(news_id,news_title,news_short,news_content,tags,lang_id) " .
					"VALUES('%s','%s', '%s', '%s', '%s', '%s')",
					TBL_NEWS_LANG, 
					$id,
					htmlentities($news['news_title'], ENT_QUOTES),
					htmlentities($news['news_short'], ENT_QUOTES),
					htmlentities($news['news_content'], ENT_QUOTES),
					htmlentities($news['tags'], ENT_QUOTES),
					$item->lang_key));
		}	
		if($res)
			return true;
		else
    		return false;	
    }
    
    function countAllNews($db,$langid)
    {
    	$sql = "SELECT COUNT(n.news_id) as total FROM ".TBL_NEWS." n ".
				"LEFT JOIN ".TBL_NEWS_LANG." nl ON n.news_id = nl.news_id AND nl.lang_id = '$langid' ".
				" ORDER BY n.news_date DESC";
    			
    	$res = $db->get_row($sql);
    	
    	return $res->total;
    }
    
    function getAllNewsSharePageAdmin($db,$langid,$share,$list)
	{
		$page = isset ( $_GET[$list] ) ? intval ( $_GET[$list] ) : 1;
		$rows_per_page= $share;
		$page_start = ( $page - 1 ) * $rows_per_page;
		$page_end = $page * $rows_per_page;
	
		$strSQL = "SELECT * FROM ".TBL_NEWS." n ".
				"LEFT JOIN ".TBL_NEWS_LANG." nl ON n.news_id = nl.news_id AND nl.lang_id = '$langid' ".
				" ORDER BY n.news_date DESC LIMIT $page_start, $rows_per_page";
		
		$res = $db->get_results($strSQL);
		if($res!=""){
				foreach($res as $item){
					$item->news_title = html_entity_decode($item->news_title);
					$item->news_short = html_entity_decode($item->news_short);
					$item->news_content = html_entity_decode($item->news_content);
				}
			}
		return $res;
	}
	
	function getNews($db, $id)
    {	
		$locale = $_SESSION["LC_ALL"];
    	$sql = "SELECT * FROM ".TBL_NEWS." n ".
				"LEFT JOIN ".TBL_NEWS_LANG." l ON n.news_id = l.news_id AND l.lang_id = '$locale' ".
				"WHERE n.news_id='$id'";
    			
    	$res = $db->get_row($sql);
    	if($res!=""){
					$res->news_title = html_entity_decode($res->news_title);
					$res->news_short = html_entity_decode($res->news_short);
					$res->news_content = html_entity_decode($res->news_content);
					$res->tags = html_entity_decode($res->tags);
			}
    	return $res;
    }
    
    function getLastTenNews($db,$langid)
    {
    	$sql = "SELECT * FROM ".TBL_NEWS." WHERE lang_id = '$langid' ORDER BY news_date DESC LIMIT 10";
    			
    	$res = $db->get_results($sql);
    	
    	return $res;
    }
    
     /**
     * Update a news.
     * @param $news is the $_POST param send by HTML form. 
     * See news HTML form in smarty template file for more about
     * form element' name.
     * @return true if query is execute successfully. Otherwise return false.
     */
	function updateNews($db, $news, $langid) {
		$sql = 
					"UPDATE ".TBL_NEWS_LANG." SET " .
					"news_title 	= '".htmlentities($news['news_title'], ENT_QUOTES)."', " .
					"news_short 	= '".htmlentities($news['news_short'], ENT_QUOTES)."'," .
					"tags 			= '".htmlentities($news['tags'], ENT_QUOTES)."'," .
					"news_content	= '".htmlentities($news['news_content'], ENT_QUOTES)."'" .
					"WHERE news_id = 	'" . $news['news_id'] ."' AND lang_id = '$langid'";
		$res1= $db->query($sql);		
    	$res = $db->query(sprintf(
					"UPDATE %s SET " .
					"news_image	    = '$news[news_image]' " .
					"WHERE news_id = " . $news['news_id'],
    				TBL_NEWS));
		
    	if($res || $res1)
			return true;
		else
    		return false;
    }
    
      /**
     * Delete a news with inputted id.
     * @param $id the news's id.
     * @return true if delete successfully otherwise return false.
     */
    function deleteNews($db, $id)
	{
		//Delete product
    	$sql = sprintf("DELETE FROM %1\$s" .
				" where %1\$s.news_id = '$id'", 
				TBL_NEWS);
		$db->query(sprintf("DELETE FROM %1\$s" .
				" where %1\$s.news_id = '$id'", 
				TBL_NEWS_LANG));
    	$res = $db->query($sql);
    	return $res;
	}
}
?>